<?php
/**
 * ShopEx licence
 * 每小时同步进销存商品库存
 * @copyright  Copyright (c) 2005-2012 ShopEx Technologies Inc. (http://www.shopex.cn)
 * @license  http://ecos.shopex.cn/ ShopEx License
 */
class b2c_tasks_goods_synstore extends base_task_abstract implements base_interface_task{

    function exec($params=null) {
        $mdl_products = app::get('b2c')->model('products');
        $filter=array(
            'jxc_syntime|lthan'=>time()-(60*30),
        );
        $bnlists = $mdl_products->getList('bn,goods_id',$filter,0,500,'jxc_syntime asc');
        if ($bnlists) {
            foreach ($bnlists as $key =>$value) {
                $_bn[$key] = $mdl_products->getBnJxc($value['bn']);
                $bn[$value['goods_id']] = $_bn[$key]['bn'];
            }
            $bn = array_unique($bn);
            foreach ($bn as $goods_id=>$val) {
                $post = array(
                    'method'=>'Cangku.getKucun',
                    'params'=>array(
                        'product_id'=>$val,
                        'kuweiName'=>'样品'
                    )
                );
                $erp_caller_obj = kernel::service('apioubao.rpc_erp_request');
                $store = $erp_caller_obj->call_api_oubao($post);
                $store = json_decode($store,true);
                $result[$goods_id] = $store['data']['cnt'];
            }
            $mdl_goods = app::get('b2c')->model('goods');
            $now_tiem = time();
            foreach ($result as $k => $v) {
                $mdl_goods->update(array('jxc_store'=>$v),array('goods_id'=>$k));
                $mdl_products->update(array('jxc_syntime'=>$now_tiem,'store'=>$v),array('goods_id'=>$k));
            }
            logger::info("自动同步进销存库存的商品 ".json_encode($result).' 时间：'.$now_tiem);
        }
    }
}

